package com.come4loves.game.module.fight.helper;

import java.util.ArrayList;
import java.util.List;

import com.come4loves.game.module.battle.helper.PBConvertHelper;
import com.come4loves.game.module.fight.action.Action;
import com.come4loves.game.module.fight.action.BatchAction;
import com.come4loves.game.module.fight.action.CardToLocationAction;
import com.come4loves.game.module.fight.constants.CardLocation;
import com.come4loves.game.module.fight.model.Card2Location;
import com.come4loves.game.module.fight.model.FightCard;
import com.come4loves.game.proto.BattleProto;

public class ActionRecorder {
	private List<Action> actions = new ArrayList<Action>();
	public void addAction(Action action) {
		this.actions.add(action);
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (Action action : actions) {
			sb.append(action.toString()).append("\n");
		}
		return sb.toString();
	}
	
	public int actionSize() {
		return this.actions.size();
	}
	
	public List<BattleProto.GeneralAct> toGeneralAct() {
		List<Action> result = new ArrayList<Action>();
		for (Action action : actions) {
			if (action instanceof BatchAction){
				BatchAction batchAction = (BatchAction)action;
				Action a = batchAction.trim();
				if (a != null) {
					result.add(a);
				}
			} else {
				result.add(action);
			}
		}
		
		return PBConvertHelper.toGeneralAct(result);
	}
	
	public void addLocation(FightCard fightCard, CardLocation location){
		Card2Location card2Location = new Card2Location(fightCard, location);
		CardToLocationAction cardToLocationAction = new CardToLocationAction(card2Location);
		this.addAction(cardToLocationAction);
	}
	public void addLocation(FightCard fightCard, CardLocation location, int target){
		Card2Location card2Location = new Card2Location(fightCard, location, target);
		CardToLocationAction cardToLocationAction = new CardToLocationAction(card2Location);
		this.addAction(cardToLocationAction);
	}
}
